﻿ 
namespace Nop.Web.Services.Easemob
{
    // ZTImage.TypeConverter
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
 

    public sealed class TypeConverter
    {
        public const long DaySecond = 86400L;

        public const long MAX_SECOND = 253402300799L;

        public static Tenum StringToEnum<Tenum>(string enu, Tenum defaultValue) where Tenum : struct
        {
            if (Enum.TryParse(enu, ignoreCase: true, out Tenum result))
            {
                return result;
            }
            return defaultValue;
        }

        public static Tenum IntToEnum<Tenum>(int enu, Tenum defaultValue) where Tenum : struct
        {
            if (Enum.TryParse(enu.ToString(), out Tenum result))
            {
                return result;
            }
            return defaultValue;
        }

        public static string ByteArrayToString(byte[] values)
        {
            if (values == null || values.Length < 0)
            {
                return "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < values.Length; i++)
            {
                stringBuilder.Append("0x" + values[i].ToString("X2"));
                if (i < values.Length - 1)
                {
                    stringBuilder.Append(",");
                }
            }
            return stringBuilder.ToString();
        }

        public static string ToUnicode(string input)
        {
            MatchCollection matchCollection = Regex.Matches(input, "([\\w]+)|(\\\\u([\\w]{4}))");
            if (matchCollection != null && matchCollection.Count > 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (Match item in matchCollection)
                {
                    string value = item.Value;
                    if (value.StartsWith("\\u"))
                    {
                        string text = value.Substring(2);
                        byte[] array = new byte[2];
                        int num = Convert.ToInt32(text.Substring(0, 2), 16);
                        int num2 = Convert.ToInt32(text.Substring(2), 16);
                        array[0] = (byte)num2;
                        array[1] = (byte)num;
                        stringBuilder.Append(Encoding.Unicode.GetString(array));
                    }
                    else
                    {
                        stringBuilder.Append(value);
                    }
                }
                return stringBuilder.ToString();
            }
            return input;
        }

        public static byte ObjectToByte(object expression)
        {
            try
            {
                return Convert.ToByte(expression);
            }
            catch
            {
                return 0;
            }
        }

        public static bool StringToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                expression = expression.ToUpper();
                switch (expression)
                {
                    case "TRUE":
                    case "YES":
                    case "OK":
                        return true;
                    case "FALSE":
                        return false;
                }
            }
            return defValue;
        }

        public static int ObjectToInt(object expression)
        {
            return ObjectToInt(expression, 0);
        }

        public static int ObjectToInt(object expression, int defValue)
        {
            if (expression != null)
            {
                return StringToInt(expression.ToString(), defValue);
            }
            return defValue;
        }

        public static decimal ObjectToDecimal(object expression)
        {
            return ObjectToDecimal(expression, decimal.Zero);
        }

        public static decimal ObjectToDecimal(object expression, decimal defValue)
        {
            if (expression != null)
            {
                return StringToDecimal(expression.ToString(), defValue);
            }
            return defValue;
        }

        public static decimal StringToDecimal(string str)
        {
            return StringToDecimal(str, decimal.Zero);
        }

        public static decimal StringToDecimal(string str, decimal defaultVal)
        {
            decimal result = default(decimal);
            if (decimal.TryParse(str, out result))
            {
                return result;
            }
            return defaultVal;
        }

        public static int StringToInt(string str)
        {
            return StringToInt(str, 0);
        }

        public static int StringToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
            {
                return defValue;
            }
            if (int.TryParse(str, out int result))
            {
                return result;
            }
            return Convert.ToInt32(StringToFloat(str, defValue));
        }

        public static byte StringToByte(string str, byte defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 4)
            {
                return defValue;
            }
            if (byte.TryParse(str, out byte result))
            {
                return result;
            }
            return defValue;
        }

        public static int[] StringToIntArray(string str, bool trans)
        {
            string[] array = str.Split(new char[1]
            {
            ','
            }, StringSplitOptions.RemoveEmptyEntries);
            List<int> list = new List<int>();
            int i = 0;
            for (int num = array.Length; i < num; i++)
            {
                if (array[i] == "0")
                {
                    list.Add(0);
                    continue;
                }
                int num2 = StringToInt(array[i], 0);
                if (num2 == 0 && trans)
                {
                    return new int[0];
                }
                list.Add(num2);
            }
            return list.ToArray();
        }

        public static long StringToLong(string str, long defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 20 || !Regex.IsMatch(str.Trim(), "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
            {
                return defValue;
            }
            if (long.TryParse(str, out long result))
            {
                return result;
            }
            return defValue;
        }

        public static long ObjectToLong(object obj, long defValue)
        {
            if (obj != null)
            {
                return StringToLong(obj.ToString(), defValue);
            }
            return defValue;
        }

        public static double ObjectToDouble(object obj, double defValue)
        {
            if (obj != null)
            {
                return StringToDouble(obj.ToString(), defValue);
            }
            return defValue;
        }

        public static float ObjectToFloat(object strValue, float defValue)
        {
            if (strValue == null)
            {
                return defValue;
            }
            return StringToFloat(strValue.ToString(), defValue);
        }

        public static float ObjectToFloat(object strValue)
        {
            return ObjectToFloat(strValue.ToString(), 0f);
        }

        public static float StringToFloat(object strValue)
        {
            if (strValue == null)
            {
                return 0f;
            }
            return StringToFloat(strValue.ToString(), 0f);
        }

        public static float StringToFloat(string strValue, float defValue)
        {
            if (strValue == null || strValue.Length > 10)
            {
                return defValue;
            }
            float result = defValue;
            if (strValue != null && Regex.IsMatch(strValue, "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
            {
                float.TryParse(strValue, out result);
            }
            return result;
        }

        public static DateTime StringToDate(string str, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(str) && DateTime.TryParse(str, out DateTime result))
            {
                return result;
            }
            return defValue;
        }

        public static DateTime StringToDate(string str)
        {
            return StringToDate(str, DateTime.Now);
        }

        public static DateTime? StringToNullableDate(string str)
        {
            if (!string.IsNullOrEmpty(str) && DateTime.TryParse(str, out DateTime result))
            {
                return result;
            }
            return null;
        }

        public static DateTime ObjectToDate(object obj)
        {
            return StringToDate(obj.ToString());
        }

        public static DateTime ObjectToDate(object obj, DateTime defValue)
        {
            if (obj == null)
            {
                obj = "";
            }
            return StringToDate(obj.ToString(), defValue);
        }

        public static bool ObjectToBool(object obj, bool defValue)
        {
            if (obj == null)
            {
                return defValue;
            }
            return StringToBool(obj.ToString(), defValue);
        }

        public static bool ObjectToBool(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            return StringToBool(obj.ToString(), defValue: false);
        }

        public static bool IntToBool(int value)
        {
            if (value <= 0)
            {
                return false;
            }
            return true;
        }

        public static long DateToLong(DateTime date)
        {
            if (date.Kind != DateTimeKind.Utc)
            {
                date = date.ToUniversalTime();
            }
            DateTime d = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            return (long)Math.Floor((date - d).TotalSeconds);
        }

        public static long DateToDayLong(DateTime date)
        {
            long num = DateToLong(date);
            return num - num % 86400;
        }

        public static DateTime LongToDate(long date)
        {
            if (date > 253402300799L)
            {
                throw new Exception("超出Unix时间戳能表示的最大秒数");
            }
            return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(date);
        }

        public static DateTime LongToDate(long date, DateTime defVal)
        {
            if (date > 253402300799L)
            {
                return defVal;
            }
            return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(date);
        }

        public static double StringToDouble(string str)
        {
            return StringToDouble(str, 0.0);
        }

        public static double StringToDouble(string str, double defaultval)
        {
            double result = 0.0;
            if (double.TryParse(str, out result))
            {
                return result;
            }
            return defaultval;
        }

        public static string IPToString(long ipaddress)
        {
            long num = ipaddress / 256 / 256 / 256;
            long num2 = num * 256 * 256 * 256;
            long num3 = (ipaddress - num2) / 256 / 256;
            long num4 = (ipaddress - num2 - num3 * 256 * 256) / 256;
            long num5 = ipaddress % 256;
            return num + "." + num3 + "." + num4 + "." + num5;
        }

        public static string NetworkIPToString(long ipaddress)
        {
            long num = ipaddress / 256 / 256 / 256;
            long num2 = num * 256 * 256 * 256;
            long num3 = (ipaddress - num2) / 256 / 256;
            long num4 = (ipaddress - num2 - num3 * 256 * 256) / 256;
            long num5 = ipaddress % 256;
            return num5 + "." + num4 + "." + num3 + "." + num;
        }

        public static long StringToIP(string ipStr)
        {
            string[] array = ipStr.Split(new char[1]
            {
            '.'
            }, StringSplitOptions.RemoveEmptyEntries);
            if (array.Length < 4)
            {
                return 0L;
            }
            long[] array2 = new long[4];
            for (int i = 0; i < 4; i++)
            {
                if (!long.TryParse(array[i], out array2[i]))
                {
                    return 0L;
                }
            }
            return ((array2[0] * 256 + array2[1]) * 256 + array2[2]) * 256 + array2[3];
        }

        public static long StringToNetworkIP(string ipStr)
        {
            string[] array = ipStr.Split(new char[1]
            {
            '.'
            }, StringSplitOptions.RemoveEmptyEntries);
            if (array.Length < 4)
            {
                return 0L;
            }
            long[] array2 = new long[4];
            for (int i = 0; i < 4; i++)
            {
                if (!long.TryParse(array[i], out array2[i]))
                {
                    return 0L;
                }
            }
            return ((array2[3] * 256 + array2[2]) * 256 + array2[1]) * 256 + array2[0];
        }

        public static string GetEnMonth(int month)
        {
            if (month <= 0 || month > 12)
            {
                month = 1;
            }
            return (new string[13]
            {
            "",
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
            })[month];
        }

        public static string GetCnMonth(int month)
        {
            return "";
        }

        public static string GetEnDay(int day)
        {
            if (day <= 0 || day > 31)
            {
                day = 1;
            }
            return (new string[32]
            {
            "",
            "1st",
            "2nd",
            "3rd",
            "4th",
            "5th",
            "6th",
            "7th",
            "8th",
            "9th",
            "10th",
            "11th",
            "12th",
            "13th",
            "14th",
            "15th",
            "16th",
            "17th",
            "18th",
            "19th",
            "20th",
            "21th",
            "22th",
            "23th",
            "24th",
            "25th",
            "26th",
            "27th",
            "28th",
            "29th",
            "30th",
            "31th"
            })[day];
        }

        public static string GetCnDay(int day)
        {
            return "";
        }

        public static Guid? StringToGuid(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return null;
            }
            if (Guid.TryParse(value, out Guid result))
            {
                return result;
            }
            return null;
        }

        public static Guid ObjectToGuid(object value)
        {
            if (value == null)
            {
                return Guid.Empty;
            }
            if (Guid.TryParse(value.ToString(), out Guid result))
            {
                return result;
            }
            return Guid.Empty;
        }

      
    }

}
